package com.example.thumbsupdemo

import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import androidx.core.view.setPadding
import kotlin.math.max

private const val TAG = "ThumbsUpView"

/**
 * 拇指和数字两部分的布局
 * 上下左右padding需要一致，否则也会按照最大值强制一致
 */
class ThumbsUpView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs),
    View.OnClickListener {
    private lateinit var mThumbView: ThumbView
    private lateinit var mCountView: CountView

    private var mChecked = false

    init {
        // 布局是横向的
        orientation = HORIZONTAL

        // 添加view
        addChild()

        // 添加点击事件
        isClickable = true
        setOnClickListener(this)
    }

    /**
     * 实际上padding为0，通过子view模拟padding
     */
    private fun addChild() {
        addThumbView()
        addCountView()
    }

    private fun addThumbView() {
        mThumbView = ThumbView(context)
        val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
//        params.marginEnd = 1.dp2px
        addView(mThumbView, params)
    }

    private fun addCountView() {
        mCountView = CountView(context)
        mCountView.setDefaultCount(9999)
        val params = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
//        params.marginStart = 1.dp2px
        addView(mCountView, params)
    }

    fun setCount(count: Int) {
        mCountView.setDefaultCount(count)
    }

    /**
     * 执行子view的动画
     */
    override fun onClick(v: View?) {
        mChecked = !mChecked
        mThumbView.onClick(mChecked)
        mCountView.onClick(mChecked)
    }

}
